# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render_to_response,redirect

from lyricsplit.models import LyricLrc,LyricLine
import logging

def home(request):
    # get a lyric line that have no anno
    # show recent add lyrics
    logging.debug('home')
    #print "a"
    ll_list = []
    for ll in LyricLrc.all().order("-add_dt").fetch(10):
        tmp_dic = {}
        tmp_dic['name'] = ll.name
        tmp_dic['key'] = ll.key()
        #tmp_dic['']
        ll_list.append(tmp_dic)
    dic = {}
    dic['ll_list'] = ll_list
    return render_to_response('lyricsplit/home.html',dic)

def add_lyric(request):
    # add a lyric page
    if request.method == "GET":
        dic = {}
        return render_to_response("lyricsplit/add_lyric.html",dic)
    else:
        # if post, get the the lyric
        lrcstr = request.POST['lrcstr']
        name = request.POST['name']

        # store the lrc
        ll = LyricLrc()
        ll.lrc_str = lrcstr
        ll.hot_num = 0
        ll.name = name
        ll.put()

        # check the format
        # store each line
        from LyricStructure.pylyric.Lyric import Lyric
        l = Lyric()
        l.loadstr(lrcstr)
        LyricLrc
        ldiclist = l.getlinelist()
        for ldic in ldiclist:
            lline = LyricLine()
            lline.time = float(ldic['time'])
            lline.line = ldic['content']
            lline.split_anno = ""
            lline.lrc = ll
            lline.put()
        return redirect('/lyricsplit/')

def annolyric(request):
    # this is to anno a file
    key = request.GET['key']
    ll = LyricLrc.get(key)
    line_list = []
    ll_list = []
    for lline in LyricLine.all().filter("lrc =",ll).order('time'):
        tmp_dic = {}
        tmp_dic['key'] = lline.key()
        tmp_dic['line'] = lline.line
        tmp_dic['split_anno'] = lline.split_anno
        tmp_dic['time'] = lline.time
        tmp_dic['anno_dt'] = lline.anno_dt
        if line_list.count(lline.line) == 0:
            line_list.append(lline.line)
        tmp_dic['sameid'] = line_list.index(lline.line)
        ll_list.append(tmp_dic)
    dic = {}
    dic['ll_list'] = ll_list
    dic['name'] = ll.name
    # get the line for current lyric and the current anno
    return render_to_response('lyricsplit/annopage.html',dic)

#@csrf_exempt
def emoannopage(request):
    # this is to anno a file
    key = request.GET['key']
    ll = LyricLrc.get(key)
    from LyricStructure.pylyric.Lyric import Lyric
    l = Lyric()
    l.loadstr(ll.lrc_str)
    line_list = l.getlinelist()
    ll_list = []
    for line in line_list:
        tmp_dic = {}
        tmp_dic['line'] = line['content']
        tmp_dic['time'] = line['time']
        ll_list.append(tmp_dic)
    dic = {}
    dic['l_key'] = key
    dic['ll_list'] = ll_list
    dic['name'] = ll.name

    # get the anno here
    return render_to_response('lyricsplit/emoanno.html',dic)

def randomemoannopage(request):
    from lyricsplit.models import EmoAnnoOk
    eao = EmoAnnoOk.all().filter("anno_ok =",0).get()
    return redirect("/lyricsplit/emoannopage?key=%s"%( str(eao.lrc.key()) ) )
